const app = getApp()
Page({
    data: {
        // 日历组件
        range: [],
        allDayValue: {},

        // 提交页面是否显示
        isShow: false,
        photo_src: '/images/activities/default.png',
        activity_name: '',
        temp_tag: '',
        start_date: '',
        end_date: '',
        duration: '',
        info: '',
        sponsor: '徒步',
        positions: [],

        activity: {},
    },
    onShow() {
        let sponsor = app.globalData.user.user_name
        this.setData({
            sponsor
        })
    },
    choosePhoto() {
        swan.chooseImage({
            count: 1,
            success: res => {
                console.log(res);
                let _photo_src = res.tempFilePaths[0]
                this.setData({
                    photo_src: _photo_src
                })
            }
        });
    },
    submit() {
        let activity = {
            photo_src: this.data.photo_src,
            activity_name: this.data.activity_name,
            temp_tag: this.data.temp_tag,
            start_date: this.data.start_date,
            end_date: this.data.end_date,
            duration: this.data.duration,
            info: this.data.info,
            sponsor: this.data.sponsor,
            positions: this.data.positions,
        }
        for (const key in activity) {
            if (activity.hasOwnProperty(key)) {
                const item = activity[key];
                // if (false) { // 测试 - 一直不让验证
                if (item.length == 0) {
                    swan.showToast({
                        title: '有字段未填写哟~',
                        icon: "none",
                    });
                    return
                }
            }
        }

        if (this.data.photo_src.indexOf('default') !== -1) {
            swan.showToast({
                title: '请上传海报图',
                icon: 'none'
            });
            return
        }
        this.setData({
            activity,
        })

        this.setData({
            isShow: true
        })

    },
    no() {
        this.setData({
            isShow: false
        })
    },
    yes() {
        // console.log('提交');
        // 将图片上传到服务器 - 图片上传不知道怎么弄
        let photo_src = this.data.photo_src
        const url = 'https://www.idear.cloud/idear/activity/save';
        app.myUploadFile(photo_src, url)
        this.setData({
            photo_src,
        })

        // 将号召活动有关字段上传到服务器
        const posturl = 'https://www.idear.cloud/idear/activity/save';
        app.myPost(posturl, this.data.activity, (err, res) => {
            if (err) {
                swan.showToast({
                    title: '号召活动失败 请重试',
                    icon: "none",
                    duration: 900
                });
            } else {
                swan.showToast({
                    title: '号召活动成功',
                    icon: "success",
                    duration: 900
                });
                this.setData({
                    isShow: false
                })
                setTimeout(() => {
                    swan.navigateBack({})
                }, 1000);
            }
        })

    },

    // 日历组件
    getRange(year, month) {
        let name = year + '-' + month
        let value = []
        const days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if ((year%4 === 0 && year%100 !== 0) || year%400 === 0) {
            days[2] = 29
        }
        for (let i = 1; i <= days[month]; i++) {
            let dayStr = i
            if (i < 10) {
                dayStr = '0'+i
            }
            const item = {
                day: year + '-' + month + '-' + dayStr,
                state: 1
            }
            value.push(item)
        }
        let _rangeItem = {
            name,
            value
        }
        // console.log(_rangeItem);
        return _rangeItem
    },
    // onLoad() {
    // onReady() {
    // onShow() {
    // 值送进去组件内，日历组件无法更新
    tmp() {
        let date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + month
        }
        let dateStr = year + '-' + month + '-' + day
        // console.log(dateStr);
        let _allDayValue = {day: dateStr, state: 1};
        // console.log(_allDayValue);
        this.setData({
            allDayValue: _allDayValue
        })

        const _rangeItem1 = this.getRange(year, month)
        if (month == 12) {
            month = 1
            year++
        } else {
            month++
        }
        const _rangeItem2 = this.getRange(year, month)
        const _rangeItems = []
        _rangeItems.push(_rangeItem1)
        _rangeItems.push(_rangeItem2)
        // console.log(_rangeItems);
        this.setData({
            range: _rangeItems
        })
        console.log(this.data.range);
        console.log(this.data.allDayValue);
    },

    inputName(e) {
        // console.log(e.detail.value);
        const _activity_name = e.detail.value
        this.setData({
            activity_name: _activity_name
        })
    },
    inputTag(e) {
        const _temp_tag = e.detail.value
        this.setData({
            temp_tag: _temp_tag
        })
    },
    inputStartDate(e) {
        const _start_date = e.detail.value
        this.setData({
            start_date: _start_date
        })
    },
    inputEndDate(e) {
        const _end_date = e.detail.value
        this.setData({
            end_date: _end_date
        })
    },
    inputDuration(e) {
        const _duration = e.detail.value
        this.setData({
            duration: _duration
        })
    },
    inputInfo(e) {
        const _info = e.detail.value
        // console.log(_info);
        this.setData({
            info: _info
        })
    },
    inputSponsor(e) {
        const _sponsor = e.detail.value
        this.setData({
            sponsor: _sponsor
        })
    },
    inputPositions(e) {
        const _positions = e.detail.value
        this.setData({
            positions: _positions
        })
    },
});